#!/bin/bash
# SPDX-License-Identifier: GPL-2.0
# Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved.
# Description: Build script for lkp patch
# Author: Huawei OS Kernel Lab
# Create: Mon Oct 23 17:05:50 2023

set -e

# cleanup conflict envs
export SRCTREE=""
export MAKEFLAGS=""
export SRC=""
export MFLAGS=""
export KBUILD_SRC=""
export KCONFIG_CONFIG=.config
export LOCALVERSION=""

# check env: CROSS_COMPILE
if [ -z "$CROSS_COMPILE" ]; then
        echo "Unknown CROSS_COMPILE"
        exit 1
fi

[ -z "$EXTLIB_OUTPUT" ] && export EXTLIB_OUTPUT=$(dirname $(readlink -f ${BASH_SOURCE[0]}))/../../..
[ -z "$_MAKE" ]                 && export _MAKE="make -j$(getconf _NPROCESSORS_ONLN)"
[ -z "$AR" ]                    && export AR=${CROSS_COMPILE}ar
[ -z "$CC" ]                    && export CC=${CROSS_COMPILE}gcc
[ -z "$LD" ]                    && export LD=${CROSS_COMPILE}ld
[ -z "$NM" ]                    && export NM=${CROSS_COMPILE}nm
[ -z "$STRIP" ]                 && export STRIP=${CROSS_COMPILE}strip

# build vmlinux
${_MAKE} -C ${EXTLIB_OUTPUT} KBUILD_LIBLINUX_FILTER_OBJS=1 CC="${CC}" vmlinux

# build in-tree modules
set -o pipefail
${_MAKE} -C ${EXTLIB_OUTPUT} CC="${CC}" modules

